<?php
/**
 * Created by PhpStorm.
 * User: yinnannan
 * Date: 16/1/19
 * Time: 上午5:15
 */
require('application/classes/api/checkcode.php');
class user  {

    public function getByUserName($parm){
        $result = DB::select('*')->from('sysuser')->where("username","=",$parm['username'])->execute()->as_array();
        return $result;
    }
    public function modifyUser($parm){

        $message = null;
        try {
            Database::instance()->begin();
            $result = DB::select('*')->from('sysuser')->where("phone", "=", $parm['phone'])->execute()->as_array();
            if (count($result) != 0) {
                $message['data'] = '手机号已经注册';
                $message['msg']  =false;
                return $message;
            }
            $result = DB::select('*')->from('sysuser')->where("email", "=", $parm['email'])->execute()->as_array();
            if (count($result) != 0) {

                $message['data'] = '邮箱已经注册';
                $message['msg']  =false;
                return $message;
            }

            date_default_timezone_set('PRC');
            $result = DB::select('*')->from('syscheckcode')->where("phone","=",$parm['phone'])->and_where('model','=','绑定手机号')->order_by("createDate","desc")->limit(1)->offset(0)->execute()->as_array();

            if(count($result)==0||time()-strtotime($result[0]['createDate'])>5*60||$result[0]['ValidateCode']!=$parm['phoneCode']){
                $message['data'] = '手机验证码不正确';
                $message['msg']  =false;
                return $message;

            }
            $result = DB::select('*')->from('syscheckcode')->where("email","=",$parm['email'])->and_where('model','=','绑定邮箱')->order_by("createDate","desc")->limit(1)->offset(0)->execute()->as_array();

            if(count($result)==0||time()-strtotime($result[0]['createDate'])>5*60||$result[0]['ValidateCode']!=$parm['emailCode']){

                $message['data'] = '邮箱验证码不正确';
                $message['msg']  =false;
                return $message;

            }

            DB::update("sysuser")->set(array('phone'=>$parm['phone'],'sex'=>$parm['sex'],'email'=>$parm['email'],'age'=>$parm['age']))->where('id','=',$parm['id'])->execute();
            $message['data'] = '修改成功';
            $message['msg']  =true;
            $result = DB::select('*')->from('sysuser')->where("phone", "=", $parm['phone'])->execute()->as_array();
            Session::instance('database')->set('user',$result[0]);
        }catch (Exception $e){
            $message['data'] = $e->getMessage();
            $message['msg']  =true;
            Database::instance()->rollback();
        }finally{
            Database::instance()->commit();
        }
        return $message;
    }
    public function getByUserPhone($parm){
        $result = DB::select('*')->from('sysuser')->where("phone","=",$parm['phone'])->execute()->as_array();
        return $result;
    }

    public function userlogin($parm){
        $result = null;
        $entity=self::getByUserPhone($parm);
        if(count($entity) == 0){
            $result['msg'] = '手机号不存在';
            $result['status'] = false;
        }else if($entity[0]['password']!=md5($parm['password'])){
            $result['msg'] = '手机号或密码错误';
            $result['status'] = false;
        }else{
            $result['status'] = true;
            $result['user']=$entity[0];
        }
        return $result;


    }
    public function modfiyPassword($parm){
        $msg = "";
        try {
            date_default_timezone_set('PRC');
            $user = self::getByUserPhone($parm);
            if (count($user) == 0 ){
                $msg = "手机号未注册";
                return ;
            }

            $checkcode = new checkcode();
            $result = $checkcode->check($parm);
            if (count($result) == 0 || $result[0]['ValidateCode'] != $parm['code']) {
                $msg = "验证码错误";

            } else if (time() - strtotime($result[0]['createDate']) > 300) {
                $msg = "验证码失效";
            } else {
                $parm['password'] = md5($parm['password']);
                Database::instance()->begin();
                DB::update("sysuser")->set(array('password' => $parm['password']))->where('id', '=', $user[0]['id'])->execute();
                $msg = "";
            }
        }catch(Exception $e){
                $msg = '注册失败请稍后再试';

            }finally{
                return $msg;
            }
    }

    public function updateUserImg($parm){
        DB::update("sysuser")->set(array('img' => $parm['img']))
            ->where('id', '=', $parm['id'])->execute();
    }
    public function regUser($parm){


        $msg = "";
        try{
            date_default_timezone_set('PRC');
            $checkcode = new checkcode();

            $result = $checkcode->check($parm);
            if(count($result)==0||$result[0]['ValidateCode']!=$parm['code']){
                $msg = "验证码错误";

            }else if(time()-strtotime($result[0]['createDate'])>300){
                $msg = "验证码失效";
            }else if(count(self::getByUserName($parm)) != 0){
                $msg="用户名已存在";

            }else if(count(self::getByUserPhone($parm)) != 0){
                $msg="手机号已存在";
            }else{
                $parm['password'] = md5($parm['password']);
                Database::instance()->begin();
                DB::insert("sysuser",array('phone','username','password'))
                    ->values(array($parm['phone'],$parm['username'],$parm['password']))
                    ->execute();
                Database::instance()->commit();
                $msg = "";
            }

        }catch(Exception $e){
            $msg = '注册失败请稍后再试';

        }finally{
            return $msg;
        }
    }

}